package per.ethan.cloud.provider.core.controller;

import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import per.ethan.cloud.common.domain.ResultBean;
import per.ethan.cloud.common.service.RedisTemplateService;
import per.ethan.cloud.provider.api.entity.Payment;
import per.ethan.cloud.provider.core.service.PaymentService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author Ethan
 * @date 2020/03/11 13:59
 */
@Slf4j
@RefreshScope
@RestController
@RequestMapping("/payment")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class PaymentController {

    private final PaymentService paymentService;

    private final DiscoveryClient discoveryClient;

    private final HttpServletRequest request;

    private final RedisTemplateService redisTemplateService;

    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/redis")
    public ResultBean<Object> getPaymentFromRedis() {
        return ResultBean.success(redisTemplateService.get("payment:429588801061523456"));
    }

    @PostMapping
    @ApiOperation(value = "添加支付信息")
    public ResultBean<Long> save(@RequestBody @Validated Payment payment) {
        return ResultBean.success(paymentService.save(payment));
    }

    @GetMapping(value = "/{id}")
    @ApiOperation(value = "通过ID查询支付信息")
    public ResultBean<Payment> getById(@PathVariable("id") Long id) {
        log.info("token: {}", request.getHeader("token"));
        return ResultBean.success(paymentService.findById(id));
    }

    @GetMapping("/hello")
    @ApiOperation(value = "hello")
    public ResultBean<String> hello() {
        return ResultBean.success("hello:" + serverPort);
    }

    @GetMapping("/feignTimeout")
    @ApiOperation(value = "休眠测试")
    public ResultBean<String> feignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResultBean.success("hello");
    }

    @GetMapping("/discovery")
    @ApiOperation(value = "服务发现")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("service: " + service);
        }
        log.info("-----------------------------------------------");

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-provider-payment");
        for (ServiceInstance instance : instances) {
            log.info(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
        }

        return this.discoveryClient;
    }

}
